package com.example.cloud.lijianhystrix.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author lj02
 * @desc
 * @date 2018/6/14
 */
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    //    @HystrixCommand(fallbackMethod = "hiError", groupKey = "hi", threadPoolKey = "hipool",
//            commandProperties = {
//                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000")
////                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "10")
////                    @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "2")//接口并发数为 2
//
//            }
//    )
    @HystrixCommand(fallbackMethod = "hiError", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000")
//                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "10")
//                    @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "2")//接口并发数为 2

    })
    public String hiService(String name) {
        if (name == null) {
            return restTemplate.getForObject("http://TEST-CLIENT2222222/showport?" + name, String.class);
        }
        String temp = restTemplate.getForObject("http://TEST-CLIENT/hi?" + name, String.class);
//        int i=1/0;
        return temp;
    }

    public String hiError(String name) {
        if (name != null) {
//            throw new RuntimeException("测试 feign-hystrix ，被调用端错误");
        }
        return "hi," + name + ",sorry,error!";
    }

}